﻿using Ch9_Portable.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Ch9RSS_WP8.Converters
{
    public class GroupToMediaConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return value;
            bool isMobile = false;
            if (parameter != null && parameter.ToString() == "mobile")
                isMobile = true;

            var group = (Group)value;
            // possible file options:
            //   video/mp4 - low
            //   video/webm - don't use
            //   video/x-ms-wmv - medium
            //   video/mp4 - high
            //   video/mp4 - medium
            //   video/x-ms-wmv - high

            // Use the highest wmv
            string mp4Option = "";
            string wmvOption = "";
            foreach (Content c in group.content)
            {
                if (c.type.EndsWith("mp4") && c.url.EndsWith("mp4"))
                    mp4Option = c.url;
                else if (c.type.EndsWith("wmv") && c.url.EndsWith("wmv"))
                    wmvOption = c.url;
                if (isMobile && wmvOption != "")
                    return wmvOption;
            }
            if (wmvOption != "")
                return wmvOption;
            else
                return mp4Option;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }
}
